package cn.passlink.www.Utils;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class CaptchaUtil {

    //生成catchCreator的map
    public static Map<String, Object> catchaImgCreator(BufferedImage bufferedImage){

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "jpg", baos);
        } catch (IOException e) {
            log.error("验证码图片构建失败", e);
            return JsonUtils.resultJson(500,"验证码图片构建失败！！！");
        }
        byte[] imageBytes = baos.toByteArray();
        // 对字节数组进行 Base64 编码
        String base64Image = Base64.getEncoder().encodeToString(imageBytes);
        // 创建 JSON 对象
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("captcha", "data:image/jpeg;base64," + base64Image);

        return JsonUtils.dataJson(200,jsonMap,"获取成功");


    }
}